#!/usr/bin/python3.9
# -*- coding: utf-8 -*-
# @Time    : 2021/10/2 20:26
# @Author  : YHSimon

import numpy as np
import matplotlib.pyplot as plt

a = np.array([[1, 2104],
              [1, 1416],
              [1, 1534],
              [1, 852]])
b = np.array([[-40, 200, -150],
              [0.25, 0.1, 0.4]])

# 获取矩阵某一行或某一列
print(a[3])  # 第四行
print(a[:, 1])  # 第2列
# 获取矩阵第一行 第三行所有的元素
print(a[[0, 2], :])
# 替换矩阵a第一列的所有元素为2
a[:, 0] = [2, 2, 2, 2]
print(a)

# 矩阵相乘
print(np.mat(a) * np.mat(b))

print(np.dot(a, b))

# 求方阵的可逆矩阵   inv逆函数  pinv伪逆函数
c = np.array([[3, 4], [2, 16]])
print(np.linalg.inv(c))
print(np.linalg.pinv(c))

# 转置矩阵
print(np.transpose(a))

# 生成单位矩阵
print(np.eye(5))

# 计算 (X的转置*X)的逆*X的转置*y

# 正态分布
mu = 1  # 期望为1
sigma = 3  # 标准差为3
num = 10000  # 个数为10000
rand_data = np.random.normal(mu, sigma, num)
count, bins, ignored = plt.hist(rand_data, 30, density=True)
plt.plot(bins, 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu) ** 2 / (2 * sigma ** 2)), linewidth=2, color='r')
# plt.show()


# 在矩阵a的右侧新增列向量
c_tmp = np.array([1, 1, 1, 1]).reshape(4, 1)
print('c_tmp', c_tmp)
print('-------------')
a = np.c_[a, c_tmp]
print(a)

# 将两个矩阵合并
d = np.array([[1, 2], [3, 4]])
e = np.array([[2, 2], [3, 3]])
print('d', d)
print('e', e)
f = np.append(d, e)
g = np.append(d, e, axis=0)  # 按行
h = np.append(d, e, axis=1)  # 按列
print(f)
print(g)
print(h)
print(np.size(d))  # 4
print(np.size(f))  # 8
print(np.size(g))  # 8
print(np.size(h))  # 8
